home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / gnu_oleo_1_2_2.lha / oleo-1.2.2 / font.c < prev    next >
C/C++ Source or Header  |  1993-03-03  |  6KB  |  274 lines

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include "font.h"
  21. #include "math.h"
  22. #include "window.h"
  23. #include "io-abstract.h"
  24.  
  25. static struct font_memo default_cell_font =
  26. {
  27.   "*times-medium-r-*",
  28.   "Times-Roman",
  29.   1.0,
  30.   0
  31. };
  32.  
  33. struct font_memo *font_list = 0;
  34.  
  35. void 
  36. init_fonts ()
  37. {
  38. }
  39.  
  40. char *default_x_name = "*times-medium-r-*";
  41. char *default_ps_name = "Times-Roman";
  42.  
  43. struct font_names
  44. {
  45.   struct font_names * next;
  46.   char * name;
  47.   char * definition;
  48.   struct font_memo * font;
  49. };
  50.  
  51. static struct font_names * font_names;
  52.  
  53. void
  54. define_font (description)
  55.      char * description;
  56. {
  57.   char * p;
  58.   char * p2;
  59.   char oleo_name[1000];
  60.   struct font_names * fn;
  61.  
  62.   for (p = description; isspace (*p); ++p);
  63.   for (p2 = oleo_name; *p && (*p != ',') && !isspace (*p); *p2++ = *p++);
  64.   *p2 = '\0';
  65.   if (oleo_name[0] == '\0')
  66.     return;
  67.   
  68.   for (fn = font_names; fn; fn = fn->next)
  69.     if (!strcmp(fn->name, oleo_name))
  70.       {
  71.     free (fn->definition);
  72.     fn->definition = 0;
  73.       }
  74.   if (!fn)
  75.     {
  76.       fn = (struct font_names *)ck_malloc (sizeof (*fn));
  77.       fn->name = strdup (oleo_name);
  78.       fn->next = font_names;
  79.       font_names = fn;
  80.     }
  81.   fn->definition = strdup (p);
  82.   fn->font = intern_font (fn->definition);
  83. }
  84.  
  85.  
  86. /* Returns NULL if the font isn't returned by any of the font_names. */
  87.  
  88. static char * 
  89. nice_name (font)
  90.      struct font_memo * font;
  91. {
  92.   struct font_names * fn;
  93.   if (!font)
  94.     return "def";
  95.   for (fn = font_names; fn; fn = fn->next)
  96.     if (fn->font == font)
  97.       return fn->name;
  98.   return 0;
  99. }
  100.  
  101. static int
  102. says_default (str)
  103.      char * str;
  104. {
  105.   char * key = "ault";
  106.   if (strincmp(str, "def", 3))
  107.     return 0;
  108.   str += 3;
  109.   while (*str && *key)
  110.     if (tolower(*str) != *key)
  111.       return 0;
  112.   while (isspace(*str))
  113.     ++str;
  114.   return !*str;
  115. }
  116.  
  117. struct font_memo *
  118. intern_font (fullname)
  119.      char *fullname;
  120. {
  121.   struct font_memo *it = font_list;
  122.   char name[1000];
  123.   char psname[1000];
  124.   char retrybuf[1000];
  125.   char *p;
  126.   char *p2;
  127.   double scale;
  128.  
  129.  retry:
  130.   for (p = fullname; isspace (*p); ++p);
  131.   if (says_default (p))        /* The specification "default" is different */
  132.     return 0;            /* from "default,".  The output of the former */
  133.                 /* changes when set_x_default_font is changed, */
  134.                 /* the latter is fixed. */
  135.   for (p2 = name; *p && (*p != ',') && !isspace (*p); *p2++ = *p++);
  136.   *p2 = '\0';
  137.   if (name[0] == '\0' || says_default(name))
  138.     strcpy (name, default_x_name);
  139.  
  140.   /* The X name might really have been a `defined font name'.
  141.    * In that case, we splice the definition in in place of the name
  142.    * and start over again.
  143.    */
  144.   {
  145.     struct font_names * fn;
  146.     for (fn = font_names; fn; fn = fn->next)
  147.       if (!strcmp (name, fn->name))
  148.     {
  149.       strcpy (retrybuf, fn->definition);
  150.       strcat (retrybuf, p);
  151.       fullname = retrybuf;
  152.       goto retry;
  153.     }
  154.   }
  155.  
  156.   while (isspace (*p))
  157.     ++p;
  158.   if (*p == ',')
  159.     ++p;
  160.   while (isspace (*p))
  161.     ++p;
  162.   for (p2 = psname; *p && (*p != ',') && !isspace (*p); *p2++ = *p++);
  163.   *p2 = '\0';
  164.   if (psname[0] == '\0' || says_default(name))
  165.     strcpy (psname, default_ps_name);
  166.   while (isspace (*p))
  167.     ++p;
  168.   if (*p == ',')
  169.     ++p;
  170.   while (isspace (*p))
  171.     ++p;
  172.   if (isdigit (*p))
  173.     {
  174.       errno = 0;
  175.       scale = atof (p);
  176.       if (errno)
  177.     scale = 1.;
  178.     }
  179.   else
  180.     scale = 1.;
  181.  
  182.   while (it)
  183.     if (   scale == it->scale
  184.     && !strcmp (name, it->name)
  185.     && !strcmp (psname, it->psname))
  186.       break;
  187.     else
  188.       it = it->next;
  189.   if (!it)
  190.     {
  191.       struct font_memo *f =
  192.     (struct font_memo *) ck_malloc (sizeof (*f));
  193.       f->scale = scale;
  194.       f->name = strdup (name);
  195.       f->psname = strdup (psname);
  196.       f->next = font_list;
  197.       font_list = f;
  198.       it = f;
  199.     }
  200.   return it;
  201. }
  202.  
  203.  
  204. /* This fills a line with the name of a font, using the defined font_names 
  205.  * if any apply. 
  206.  */
  207. void
  208. set_line_to_nice_font_name (line, font)
  209.      struct line * line;
  210.      struct font_memo * font;
  211. {
  212.   char * nice = nice_name (font);
  213.   if (nice)
  214.     set_line (line, nice);
  215.   else
  216.     sprint_line (line, "%s, %s, %f", font->name, font->psname, font->scale);
  217. }
  218.  
  219. void 
  220. set_area_font (rng, font)
  221.      struct rng *rng;
  222.      struct font_memo *font;
  223. {
  224.   CELL * cp;
  225.   make_cells_in_range (rng);
  226.   cp = next_cell_in_range ();
  227.   while (cp)
  228.     {
  229.       cp->cell_font = font;
  230.       cp = next_cell_in_range ();
  231.     }
  232.   io_redo_region (rng);
  233. }
  234.  
  235. void 
  236. flush_fonts ()
  237. {
  238.   CELL * cp;
  239.   struct rng rng;
  240.   rng.lr = MIN_ROW;
  241.   rng.hr = MAX_ROW;
  242.   rng.lc = MIN_COL;
  243.   rng.hc = MAX_COL;
  244.   find_cells_in_range (&rng);
  245.   cp = next_cell_in_range ();
  246.   while (cp)
  247.     {
  248.       cp->cell_font = 0;
  249.       cp = next_cell_in_range ();
  250.     }
  251. }
  252.  
  253. struct font_memo * 
  254. default_font ()
  255. {
  256.   return &default_cell_font;
  257. }
  258.  
  259. void
  260. set_x_default_font (str)
  261.      char * str;
  262. {
  263.   default_cell_font.name = strdup(str);
  264.   io_repaint ();
  265. }
  266.  
  267. void
  268. set_ps_font_cmd (name)
  269.      char * name;
  270. {
  271.   /* A deliberate core leak so we can init with a constant. */
  272.   default_cell_font.psname = strdup (name);
  273. }
  274.